package org.zero.onlinesubject.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zero.onlinesubject.common.vo.MessageNotifyVo;
import org.zero.onlinesubject.common.vo.Result;
import org.zero.onlinesubject.entity.MessageType;
import org.zero.onlinesubject.entity.User;
import org.zero.onlinesubject.service.MessageNotifyService;
import org.zero.onlinesubject.utils.UserContextHolder;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

@RestController
@RequestMapping("/message/api")
public class MessageController {

    @Resource
    private MessageNotifyService msgNotifyService;

    // 获取消息类型
    @GetMapping("/msgTypeList")
    public Result<List<MessageType>> getMsgTypeList(){
        List<MessageType> msgTypeList = msgNotifyService.getMsgTypeList();
        return Result.ok(msgTypeList);
    }

    // 消息中心
    @GetMapping("/center")
    public Result<List<MessageNotifyVo>> getMessages(Integer current,Integer size) throws ExecutionException, InterruptedException, TimeoutException {
        List<MessageNotifyVo> voList = msgNotifyService.getMessages();
        return Result.ok(voList);
    }

    // 用户通过关键词搜索过滤消息
    @GetMapping("/searchMsg")
    public Result<List<MessageNotifyVo>> getMessageByName(String msgName,Boolean isRead,Integer current,Integer size){
        if(size == null || size == 0) size = 10;
        if(current == null)
            current = 0;
        else
            current = (current - 1 ) * size;
        List<MessageNotifyVo> voList = msgNotifyService.getMessageByName(msgName,isRead,current,size);
        return Result.ok(voList);
    }

    // 通知消息确认
    @GetMapping("/msgConf")
    public Result<String> confirmMsg(Integer msgId){
        User user = UserContextHolder.get();
        Boolean flag = msgNotifyService.updateMsgState(msgId,user.getUserid());
        return flag ? Result.ok("更新消息状态成功") : Result.error(500,"更新消息状态失败");
    }

    // 获取通知消息详情
    @GetMapping("/msgInfo")
    public Result<MessageNotifyVo> getNotifyInfo(Integer notifyid){
         MessageNotifyVo vo = msgNotifyService.getNotifyInfo(notifyid);
         return Result.ok(vo);
    }
}
